<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ CMS ]
 */
namespace CMS\Controller;
use Common\Controller\AdminController;
Vendor('Qiniusdk.autoload'); //七牛入口文件引入 
use Qiniu\Auth; 
use Qiniu\Storage\UploadManager;
use Think\Upload;
use Qiniu\Processing\PersistentFop;

/**
 * 页面
 */
class PageController extends AdminController{
	
	 public function ceshi(){
		
        Vendor('Qny.autoload');
        // 用于签名的公钥和私钥
        $accessKey = '8sc_uncdqBnQUYyWViRALobZH33YO_lZKD65Y04a';
        $secretKey = 'FczTgs6OmHBb7jke7pExGeujvxIHXO0trUCIbmBE';
        // 初始化签权对象
        $auth = new Auth($accessKey, $secretKey);
        // 空间名  https://developer.qiniu.io/kodo/manual/concepts
//        p78151fwl.bkt.clouddn.com
        $bucket = 'music';
        // 生成上传Token
        $token = $auth->uploadToken($bucket);
		
		$this->assign("token",$token);
		 $this->display();
	 }
	
	
	/**
	 * 页面管理
	 */
    public function Manage(){

		//页面配置
		$page = array(
			
			'title'=>'页面管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		
		);
		
		$this->assign('page',$page);
		//开始结束时间
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			
			$map['time']  = array(array('egt',$begin_time),array('elt',$end_time),'and'); 
			
		}else{
			
			//默认今天明天
			$begin_time=time();
			$end_time=time()+86400;
		}
		
		//判断是否搜索标题
		$name = I('param.name');
		if($name){
			
			$map['name'] = array('like',"%$name%");

		}
		$this->assign('name',$name);
		
		//用户UID
		$uid = I('param.uid')?I('param.uid'):'';

		if($uid!=0){
			
			$map['uid'] =array('like',"%$uid%");
		}
		
		//selected="selected"
		$this->assign('uid',$uid);
		
		
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		$m_cms_content = M('cms_content');
		$m_common_user = M('common_user');
		
		//获得上级信息
		$fmap['id'] = I('param.form_id')?I('param.form_id'):0;
		$fmap['type'] = get_cms_model_conf('CMS_PAGE');
		$fmap['status'] = 1;
		
		$data = $m_cms_content->where($fmap)->find();
		
		$search = I('param.search');
		//判断是否是搜索
		if(!$search){
			
			$map['form_id'] = $fmap['id'];
			
		}
		
		$map['type'] = get_cms_model_conf('CMS_PAGE');
		$map['status'] = 1;
		
		$_GET['p']=$_GET['p']?$_GET['p']:1;
		
		$list = $m_cms_content->where($map)->order('sort desc')->page($_GET['p'].',50')->select();
		$count      = $m_cms_content->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		//查询关联信息
		foreach($list as $i=>$k){
			
			
			$umap['id'] = $list[$i]['uid'];
			
			$udata = $m_common_user->where($umap)->find();
			
			$map_num['form_id'] = $list[$i]['id'];
			$map_num['type'] = get_cms_model_conf('CMS_PAGE');;
			$map_num['status'] = 1;
			$list[$i]['num'] = $m_cms_content->where($map_num)->count();
			
			$list[$i]['nickname'] = $udata['nickname'];
			$list[$i]['img'] = $udata['img'];
			
		
		}
		//赋值
		$this->assign('list',$list);
		$this->assign('data',$data);
		$this->assign('form_id',$fmap['id']);
		
		
		$this->display();
	}
	
	/**
	 * 新增页面
	 */
	public function Create(){
		

		//判断是否是ajax请求
		if(IS_POST){

			$m_cms_content = M('cms_content');
			
			
			//获得数据
			$data = I();
			
			$data['value'] = I('param.value','','');
			$data['uid'] = getUID();
			$data['form_id'] =  I('param.form_id')?I('param.form_id'):0;;
			$data['type'] = get_cms_model_conf('CMS_PAGE');
			$data['time'] = time();
			$data['ip'] = get_client_ip();
			$data['status'] = 1;
			
			$result = $m_cms_content->add($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
			
				$this->success('新增成功', U('CMS/Page/Manage',array('form_id'=>$data['form_id'])));
			
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置  
			
				$this->error('新增失败');
			
			}
		
				
		}else{
			
			$form_id =  I('param.form_id')?I('param.form_id'):0;;
			
			$this->assign('form_id',$form_id);
		
		
			$this->display();
		}
		
	
	}
	
	/**
	 * 页面详情
	 */
	public function Detail(){
    
		$m_cms_content = M('cms_content');
		
		common_public()->assign_page('页面详情');
		
		$map['id'] = I('param.id');
		$map['type'] = get_cms_model_conf('CMS_PAGE');
		$map['status'] = 1;
			
		$data = $m_cms_content->where($map)->find();
			
		$this->assign('data',$data);
		
			
		$this->display();
		
		
	
	}
	/**
	 * 编辑页面
	 */
	public function Edit(){
    
		$m_cms_content = M('cms_content');
		
		$map['id'] = I('param.id');
		//判断是否是ajax请求
		if(IS_POST){

			$m_cms_content = M('cms_content');
			
			set_cms_draft($map['id']); //保存草稿
			
			$data = I('');
			unset($data['id']);
			$data['value'] = I('param.value','','');
			$data['time'] = time();
			$data['ip'] = get_client_ip();
			
			$result = $m_cms_content->where($map)->save($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
			
				//$this->success('编辑成功', U('CMS/Page/Manage'));
				$this->success('编辑成功', U('CMS/Page/Manage',array('form_id'=>$data['form_id'])));
			
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置  
			
				$this->error('编辑失败');
			
			}
		
		}else{
			
			$map['type'] = get_cms_model_conf('CMS_PAGE');
			$map['status'] = 1;
		
		
			$data = $m_cms_content->where($map)->find();
			
			$this->assign('data',$data);
		
			
			$this->display();
		}
		
	
	}
	
	/**
	 * 批量改变页面状态
	 */
	public function Delete($id,$status=0){
		
	
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_cms_content = M('cms_content');
			
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			if($id){
				
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					
					$map['id'] = $id[$i];
					$data['status'] = $status;
					
					$m_cms_content->where($map)->save($data);
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	
	/**
	 * 页面管理
	 */
    public function banner(){

		//页面配置
		$page = array(
			'title'=>'banner管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		);
		
		$this->assign('page',$page);
		//开始结束时间
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			$map['time']  = array(array('egt',$begin_time),array('elt',$end_time),'and');
		}else{
			//默认今天明天
			$begin_time=time();
			$end_time=time()+86400;
		}
		
		//判断是否搜索标题
		$name = I('param.name');
		if($name){
			
			$map['name'] = array('like',"%$name%");

		}
		$this->assign('name',$name);
		
		//判断是否搜索页面
		$form_id = I('param.form_id');
		if($form_id){
			$map['form_id'] = $form_id;
		}
		$this->assign('form_id',$form_id);
		
		//判断是否搜索风格
		$style_id = I('param.style_id');
		if($style_id){
			$map['style_id'] = $style_id;
		}else{
			$map['style_id'] = array("neq",0);
		}
		$this->assign('style_id',$style_id);
		
		//selected="selected"
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		$m_cms_content = M('cms_content');
		$m_common_user = M('common_user');
		$m_mall_content = M('mall_content');
		
		$map['type'] = get_cms_model_conf('CMS_BANNER');
		$map['status'] = 1;
		
		$_GET['p']=$_GET['p']?$_GET['p']:1;
		
		$list = $m_cms_content->where($map)->order('sort desc')->page($_GET['p'].',50')->select();
		//dump($map);die;
		$count      = $m_cms_content->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		//查询关联信息
		foreach($list as $i=>$k){
			$umap['id'] = $list[$i]['uid'];
			$udata = $m_common_user->where($umap)->find();
			
			$ffmap['id'] = $list[$i]['form_id'];
			$ffdata = $m_cms_content->where($ffmap)->find();
			
			$list[$i]['form_name'] = $ffdata['name'];
			$list[$i]['nickname'] = $udata['nickname'];
			
			$style_array = explode(',',$list[$i]['style_id']);
			foreach($style_array as $ii=>$vv){
				$style_map['id'] = $style_array[$ii];
				$style_data = $m_mall_content->where($style_map)->find();
				$style_name_array[] = $style_data['name'];
			}
			
			$list[$i]['style_name'] = implode(',',$style_name_array);
			$list[$i]['style_name'] = style_str($list[$i]['style_id']);
			
		}
		//dump($list);die;
		//赋值
		$this->assign('list',$list);
		$this->assign('data',$data);
		$this->assign('form_id',$fmap['id']);
		
		#风格
		$style_maps['type'] = get_mall_model_conf('MALL_STYLE');
		$style_maps['status'] = 1;
		$style_list = $m_mall_content->where($style_maps)->order('sort desc')->select();
		$this->assign('style_list',$style_list);
		
		#页面配置
		$page_map['type'] = get_cms_model_conf('CMS_BANNER_STYLE');
		$page_map['status'] = 1;
		
		$page_list = $m_cms_content->where($page_map)->order('sort desc')->select();
		$this->assign('page_list',$page_list);
		
		$this->display();
	}
	
	/**
	 * 页面管理
	 */
    public function default_banner(){

		//页面配置
		$page = array(
			'title'=>'banner管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		);
		
		$this->assign('page',$page);
		//开始结束时间
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			$map['time']  = array(array('egt',$begin_time),array('elt',$end_time),'and');
		}else{
			//默认今天明天
			$begin_time=time();
			$end_time=time()+86400;
		}
		
		//判断是否搜索标题
		$name = I('param.name');
		if($name){
			
			$map['name'] = array('like',"%$name%");

		}
		$this->assign('name',$name);
		
		//判断是否搜索页面
		$form_id = I('param.form_id');
		if($form_id){
			$map['form_id'] = $form_id;
		}
		$this->assign('form_id',$form_id);
		
		//selected="selected"
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		$m_cms_content = M('cms_content');
		$m_common_user = M('common_user');
		$m_mall_content = M('mall_content');
		
		$map['type'] = get_cms_model_conf('CMS_BANNER');
		$map['status'] = 1;
		$map['style_id'] = 0;
		$_GET['p']=$_GET['p']?$_GET['p']:1;
		//dump($map);die;
		
		$list = $m_cms_content->where($map)->order('sort desc')->page($_GET['p'].',50')->select();
		
		$count      = $m_cms_content->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		//查询关联信息
		foreach($list as $i=>$k){
			$umap['id'] = $list[$i]['uid'];
			$udata = $m_common_user->where($umap)->find();
			
			$ffmap['id'] = $list[$i]['form_id'];
			$ffdata = $m_cms_content->where($ffmap)->find();
			
			$list[$i]['form_name'] = $ffdata['name'];
			$list[$i]['nickname'] = $udata['nickname'];
			
			$style_map['id'] = $list[$i]['style_id'];
			$style_data = $m_mall_content->where($style_map)->find();
			$list[$i]['style_name'] = $style_data['name'];
			
			/* $save_pm['img'] = $list[0]['img'];
			$save_id['id'] = $list[$i]['id'];
			 $m_cms_content->where($save_id)->save($save_pm); */
			
		}
		//赋值
		$this->assign('list',$list);
		$this->assign('data',$data);
		$this->assign('form_id',$fmap['id']);
		
		#页面配置
		$page_map['type'] = get_cms_model_conf('CMS_BANNER_STYLE');
		$page_map['status'] = 1;
		$page_list = $m_cms_content->where($page_map)->order('sort desc')->select();
		$this->assign('page_list',$page_list);
		$this->display();
	}
	
	/* 新增banner */
	public function Create_banner(){
		
		$m_cms_content = M('cms_content');
		$m_mall_content = M('mall_content');
		//判断是否是ajax请求
		if(IS_POST){
			//获得数据
			$data = I();
			$style = I('param.style_id');
			$data['style_id'] = implode(',',I('param.style_id'));
			if(!$data['style_id']){
				unset($data['style_id']);
			}
			$data['uid'] = getUID();
			$data['type'] = get_cms_model_conf('CMS_BANNER');
			$data['time'] = time();
			$data['ip'] = get_client_ip();
			$data['status'] = 1;
			//dump($data);die;
			$result = $m_cms_content->add($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']\
				add_style(4,$result,$style); 
				$this->success('新增成功', U('CMS/Page/Banner'));
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置
				$this->error('新增失败');
			}	
		}else{
			#banner选择的页面
			$type_map['type'] = get_cms_model_conf("CMS_BANNER_STYLE");
			$type_map['status'] = 1;
			$banner_type = $m_cms_content->where($type_map)->field("id,name")->select();
			#活动页面
			$activity_map['type'] = get_mall_model_conf('MALL_ACTIVITY');
			$activity_map['status'] = 1;
			$activity_list = $m_mall_content->where($activity_map)->select();
			#专辑页面
			$album_map['type'] = get_mall_model_conf('MALL_ALBUM');
			$album_map['status'] = 1;
			$album_list = $m_mall_content->where($album_map)->select();
			#单曲试听页面
			$song_map['type'] = get_mall_model_conf('MALL_SINGLE');
			$song_map['status'] = 1;
			$song_list = $m_mall_content->where($song_map)->select();
			#风格
			$style_map['type'] = get_mall_model_conf('MALL_STYLE');
			$style_map['status'] = 1;
			$style_list = $m_mall_content->where($style_map)->order('sort desc')->select();
			
			$this->assign('banner_type',$banner_type);
			$this->assign('album_list',$album_list);
			$this->assign('activity_list',$activity_list);
			$this->assign('song_list',$song_list);
			$this->assign('style_list',$style_list);
			
			$this->display();
		}
		
		
	}
	
	/* 编辑banner */
	public function Edit_banner(){
		$m_cms_content = M("cms_content");
		$m_mall_content = M("mall_content");
		
		$map['id'] = I("param.id");
		if(IS_POST){
			//获得数据
			$data = I();
			$style = I('param.style_id');
			$data['style_id'] = implode(',',I('param.style_id'));
			
			unset($data['id']);
			$data['uid'] = getUID();
			$data['time'] = time();
			$data['ip'] = get_client_ip();
			$data['status'] = 1;
			$result = $m_cms_content->where($map)->save($data);
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
				add_style(4,$map['id'],$style); 
				$this->success('编辑成功', U('CMS/Page/Banner'));
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置
				$this->error('编辑失败');
			}
		}else{
		
			#banner选择的页面
			$type_map['type'] = get_cms_model_conf("CMS_BANNER_STYLE");
			$type_map['status'] = 1;
			$banner_type = $m_cms_content->where($type_map)->field("id,name")->select();
			#活动页面
			$activity_map['type'] = get_mall_model_conf('MALL_ACTIVITY');
			$activity_map['status'] = 1;
			$activity_list = $m_mall_content->where($activity_map)->select();
			#专辑页面
			$album_map['type'] = get_mall_model_conf('MALL_ALBUM');
			$album_map['status'] = 1;
			$album_list = $m_mall_content->where($album_map)->select();
			#单曲试听页面
			$song_map['type'] = get_mall_model_conf('MALL_SINGLE');
			$song_map['status'] = 1;
			$song_list = $m_mall_content->where($song_map)->select();
			#风格
			$style_map['type'] = get_mall_model_conf('MALL_STYLE');
			$style_map['status'] = 1;
			$style_list = $m_mall_content->where($style_map)->order('sort desc')->select();
			
			
			$map['status'] = 1;
			$data = $m_cms_content->where($map)->find();
			
			$style_list = if_style($style_list,$data['style_id']);
			//dump($data);die;
			$this->assign('banner_type',$banner_type);
			$this->assign('album_list',$album_list);
			$this->assign('activity_list',$activity_list);
			$this->assign('song_list',$song_list);
			$this->assign('style_list',$style_list);
			$this->assign('data',$data);
			$this->display();
				
		}
	}
	
	
	/* 编辑banner */
	public function Edit_d_banner(){
		$m_cms_content = M("cms_content");
		$m_mall_content = M("mall_content");
		
		$map['id'] = I("param.id");
		if(IS_POST){
			//获得数据
			$data = I();
			unset($data['id']);
			$data['uid'] = getUID();
			$data['time'] = time();
			$data['ip'] = get_client_ip();
			$data['status'] = 1;
			$result = $m_cms_content->where($map)->save($data);
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
				$this->success('编辑成功', U('CMS/Page/Banner'));
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置
				$this->error('编辑失败');
			}
		}else{
		
			#banner选择的页面
			$type_map['type'] = get_cms_model_conf("CMS_BANNER_STYLE");
			$type_map['status'] = 1;
			$banner_type = $m_cms_content->where($type_map)->field("id,name")->select();
			#活动页面
			$activity_map['type'] = get_mall_model_conf('MALL_ACTIVITY');
			$activity_map['status'] = 1;
			$activity_list = $m_mall_content->where($activity_map)->select();
			#专辑页面
			$album_map['type'] = get_mall_model_conf('MALL_ALBUM');
			$album_map['status'] = 1;
			$album_list = $m_mall_content->where($album_map)->select();
			#单曲试听页面
			$song_map['type'] = get_mall_model_conf('MALL_SINGLE');
			$song_map['status'] = 1;
			$song_list = $m_mall_content->where($song_map)->select();
			#风格
			$style_map['type'] = get_mall_model_conf('MALL_STYLE');
			$style_map['status'] = 1;
			$style_list = $m_mall_content->where($style_map)->order('sort desc')->select();
			
			
			$map['status'] = 1;
			$data = $m_cms_content->where($map)->find();
			//dump($data);die;
			$this->assign('banner_type',$banner_type);
			$this->assign('album_list',$album_list);
			$this->assign('activity_list',$activity_list);
			$this->assign('song_list',$song_list);
			$this->assign('style_list',$style_list);
			$this->assign('data',$data);
			$this->display();
				
		}
	}
	
	
	/* 排序单曲推荐位置 */
	public function r_activity(){
		$m_cms_content = M('cms_content');
		if(IS_AJAX){
			
			$map['id'] = I("param.id");
			$data['sort'] = I("param.sort");
			if(!empty($map['id'])){
				$result = $m_cms_content->where($map)->save($data);
				if($result){
					$info = 1;
				}else{
					$info = 2;
				}
			}else{
				$info = 3;
			}
			
		}else{
			$info = 4;
		}
		$this->ajaxReturn($info);
	}
		
	
	
}